﻿using System.Windows;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using RssReaderTemplate.ViewModels;
using System;
using System.Linq;
using Microsoft.Phone.Shell;
using RssReaderTemplate.Globalization;

namespace RssReaderTemplate.Views
{
    public partial class Details : PhoneApplicationPage
    {
        private DetailsViewModel _viewModel;
        public Details()
        {
            InitializeComponent();
            BuildApplicationBar();
        }

        public void BuildApplicationBar()
        {
            ApplicationBarIconButton button = new ApplicationBarIconButton(new Uri("/icons/appbar.back.rest.png", UriKind.Relative))
            {
                Text = AppResources.Previous
            };
            button.Click += Previous_Click;
            ApplicationBar.Buttons.Add(button);
            button = new ApplicationBarIconButton(new Uri("/icons/Categories.png", UriKind.Relative))
            {
                Text = AppResources.Categories,
            };
            button.Click += Categories_Click;
            ApplicationBar.Buttons.Add(button);
            button = new ApplicationBarIconButton(new Uri("/icons/appbar.folder.rest.png", UriKind.Relative))
            {
                Text = AppResources.List
            };
            button.Click += List_Click;
            ApplicationBar.Buttons.Add(button);
            button = new ApplicationBarIconButton(new Uri("/icons/appbar.next.rest.png", UriKind.Relative))
            {
                Text = AppResources.Next
            };
            button.Click += Next_Click;
            ApplicationBar.Buttons.Add(button);
        }

        protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
        {
            this.State["ViewModel"] = _viewModel;
            _viewModel.Dispose();
            base.OnNavigatingFrom(e);
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            object viewModel;
            if (!State.TryGetValue("ViewModel", out viewModel))
            {
                int index = int.Parse(NavigationContext.QueryString["index"]);
                Guid guid = new Guid(NavigationContext.QueryString["id"]);
                _viewModel = ViewModelCollection.DetailsViewModel(guid,index);
            }
            else
            {
                _viewModel = viewModel as DetailsViewModel;
            }
            DataContext = _viewModel;
            base.OnNavigatedTo(e);
        }

        private void Previous_Click(object sender, EventArgs e)
        {
            int index = _viewModel.Index-1;
            if (index >= 0)
            {
                var id = _viewModel.Items.ElementAt(index).Id.ToString();
                NavigationService.Navigate(new Uri(string.Format("/Views/Details.xaml?index={0}&id={1}", index, id), UriKind.Relative));
            }
        }

        private void Next_Click(object sender, EventArgs e)
        {
            int index = _viewModel.Index +1;
            if (index<_viewModel.Items.Count)
            {
                var id = _viewModel.Items.ElementAt(index).Id.ToString();
                NavigationService.Navigate(new Uri(string.Format("/Views/Details.xaml?index={0}&id={1}", index, id), UriKind.Relative));
            }
        }

        private void Categories_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/Categories.xaml", UriKind.Relative));
        }

        private void List_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/MainPage.xaml",UriKind.Relative));
        }
    }
}